package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.instance.entity.OffWork;
import com.a01_2021.thousandstimes.instance.service.IOffWorkService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-21
 */
@RestController
@RequestMapping("/offWork")
public class OffWorkController {

    @Resource
    IOffWorkService offWorkService;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    /**
     * 用于员工请假
     *
     * @param instanceId  实例id
     * @param token       jwt令牌
     * @param offWorkDate 请假时间
     * @param type        请假类型
     * @param reason      请假理由
     * @return AjaxResponse 请假实体类
     * @throws CustomException 不应当抛出该异常
     */
    @PreAuthorize("hasAuthority('offWork-request')")
    @DS("#header.instanceid")
    @PostMapping("request")
    public AjaxResponse request(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, LocalDate offWorkDate, int type, String reason) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        OffWork offWork = offWorkService.request(userId, offWorkDate, type, reason);
        return AjaxResponse.success(offWork);
    }

    /**
     * 获取用户请假记录
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @return AjaxResponse 用户请假记录
     * @throws CustomException 不应当抛出该异常
     */
    @PreAuthorize("hasAuthority('offWork-getRequests')")
    @DS("#header.instanceid")
    @PostMapping("getRequests")
    public AjaxResponse getRequests(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        List<OffWork> offWorkList = offWorkService.getRequests(userId);
        return AjaxResponse.success(offWorkList);
    }

    /**
     * 获取全部请假记录
     *
     * @param instanceId 实例id
     * @return AjaxResponse 全部请假记录
     */
    @PreAuthorize("hasAuthority('offWork-getAllRequests')")
    @DS("#header.instanceid")
    @PostMapping("getAllRequests")
    public AjaxResponse getAllRequests(@RequestHeader("instanceid") String instanceId) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        List<OffWork> offWorkList = offWorkService.getAllRequests();
        return AjaxResponse.success(offWorkList);
    }

    /**
     * 获取全部未处理请假
     *
     * @param instanceId 实例id
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('offWork-getAllUnHandleRequests')")
    @DS("#header.instanceid")
    @PostMapping("getAllUnHandleRequests")
    public AjaxResponse getALLUnHandleRequests(@RequestHeader("instanceid") String instanceId) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        List<OffWork> offWorkList = offWorkService.getALLUnHandleRequests();
        return AjaxResponse.success(offWorkList);
    }

    /**
     * 处理请假请求
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @param requestId  请求id
     * @param stats      请求处理状态
     * @return AjaxResponse
     * @throws CustomException 当请假请求不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('offWork-handleRequest')")
    @DS("#header.instanceid")
    @PostMapping("handleRequest")
    public AjaxResponse handleRequest(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, long requestId, int stats) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));//获取用户id
        OffWork offWork = offWorkService.handleRequest(userId, requestId, stats);
        return AjaxResponse.success(offWork);
    }
}
